package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.mapper.SupDetailMapper;
import cn.tedu.csmall.product.pojo.dto.SpuAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.vo.SpuStandardVO;
import cn.tedu.csmall.product.service.ISupService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class SupServiceImpl implements ISupService, ServiceCode {
    @Autowired
    SpuMapper spuMapper;
    public SupServiceImpl() {
        log.debug("创建业务类:SupServiceImpl");
    }
    @Override
    public void andNew(SpuAndNewDTO spuAndNewDTO) {
        log.debug("开始添加业务类:SupServiceImpl");
        Spu spu=new Spu();
        LocalDateTime now=LocalDateTime.now();
        BeanUtils.copyProperties(spuAndNewDTO,spu);
        spu.setGmtCheck(now);
        spu.setGmtModified(now);
        int insert = spuMapper.insert(spu);
        if (insert!=1){
            String message="添加数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }
    @Override
    public void delete(Long id) {
    log.debug("开始处理删除功能");
        SpuStandardVO spu = spuMapper.getSpu(id);
        if (spu==null){
            String message="删除数据失败！"+id+"该数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int delete = spuMapper.delete(id);
        if (delete!=1){
            String message="删除数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
